<?php

class Application_Model_Mapper_FriendlistMapper
{
	protected $_dbTable;

	public function setDbTable ($dbTable)
	{
		
		if (is_string($dbTable))
		{
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract)
		{
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
		
	}

	public function getDbTable ()
	{
		
		if (null === $this->_dbTable)
		{
			$this->setDbTable('Application_Model_DbTable_Friendlist');
		}
		return $this->_dbTable;
		
	}

	public function add (Application_Model_Friendlist $friend)
	{	
	
		$now = new DateTime();
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
			$data = array(
				'id' => $generatedid,
				'created' => date('Y-m-d H:i:s'),
				'userid' => $friend->getUserid(),
				'friendid' => $friend->getFriendid(),
				'akzeptiert' => '0',
				);
		
			return $this->getDbTable()->insert($data);
		}
		else{
			$this->add($friend);
		}
	}

	public function activate($id)
	{
		$data = array(
				'akzeptiert' => true);
		return $this->getDbTable()->update($data, array(
				'id = ?' => $id));
	}
	
	public function activateusersichtbar($id, $status)
	{
		$data = array(
				'usersichtbar' => $status);
		return $this->getDbTable()->update($data, array(
				'id = ?' => $id));
	}

	public function activatefriendsichtbar($id, $status)
	{
		$data = array(
				'friendsichtbar' => $status);
		return $this->getDbTable()->update($data, array(
				'id = ?' => $id));
	}
	
	public function delete ($id)
	{
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$table->delete($where);
		
	}

	public function fetchID($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('friendlist');
		$entry->where('friendlist.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function fetchlist($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('friendlist');
		$entry->where('friendlist.userid = ?', $id);
		$entry->orwhere('friendlist.friendid = ?', $id);
		return $entry->query()->fetchall();
	}

	public function fetchlistrequest($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('friendlist');
		$entry->where('friendlist.userid = ?', $id);
		$entry->where('friendlist.akzeptiert = 0');

		$entry->joinleft(
				array('users' => 'users'),
				'friendlist.friendid = users.id',
				array('vorname As freundname'));
		
		return $entry->query()->fetchall();
	}
	
	public function fetchlistreply($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('friendlist');
		$entry->where('friendlist.friendid = ?', $id);
		$entry->where('friendlist.akzeptiert = 0');

		$entry->joinleft(
				array('users' => 'users'),
				'friendlist.userid = users.id',
				array('vorname As freundname'));
		
		return $entry->query()->fetchall();
	}

	public function fetchmyfriends($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('friendlist');
		$entry->where('friendlist.userid = ?', $id);
		$entry->orwhere('friendlist.friendid = ?', $id);
		$list = $entry->query()->fetchall();
		
		
		$listback = array();
		$mapper = new Application_Model_Mapper_AuthMapper();
		$i = 0;
		foreach ($list as $array){
			if($array['akzeptiert']){
				if($array['friendid'] == $id){
					$listback[$i]['freundid'] = $array['userid'];
					$listback[$i]['freundname'] = $mapper->fetchVorname($array['userid']);
					$listback[$i]['sichtbar'] = $array['friendsichtbar'];
				}
				elseif($array['userid'] == $id){
					$listback[$i]['freundid'] = $array['friendid'];
					$listback[$i]['freundname'] =  $mapper->fetchVorname($array['friendid']);
					$listback[$i]['sichtbar'] = $array['usersichtbar'];
				}
				$listback[$i]['id'] = $array['id'];
				$listback[$i]['created'] = $array['created'];
					
				++$i;
			}
		
		}
		return $listback;
	}
	
	public function usermoechtepinnwandeintrag($id, $friend)
	{		
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('friendlist');
		$entry->where('friendlist.userid = ?', $id);
		$entry->where('friendlist.friendid = ?', $friend);

		$entry = $entry->query()->fetch();
		if(count($entry)>1){
			return $entry['friendsichtbar'];			
		}
		
		
		$entry1 = new Zend_Db_Select($db);
		$entry1->from('friendlist');
		$entry1->where('friendlist.userid = ?', $friend);
		$entry1->where('friendlist.friendid = ?', $id);
		
		$entry1 = $entry1->query()->fetch();
		if(count($entry1)>0){
			return $entry1['usersichtbar'];
		}
		return false;
		
	}
	
	public function arefriends($id, $friend)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		// 		$entry = $this->getDbTable();
		$entry->from('friendlist');
		$entry->where('friendlist.userid = ?', $id);
		$entry->where('friendlist.friendid = ?', $friend);
		$entry->orwhere('friendlist.userid = ?', $friend);
		$entry->where('friendlist.friendid = ?', $id);
		return $entry->query()->fetch();
	/*	if(!empty($test['id'])){
			return false;
		}
		else{
			return true;
		}
	*/
	}

}